package com.oa.tools.utils;

import java.io.File;
import java.io.FileInputStream;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ToolConfig
{
  public static String domainName = "http://oa.fengbangleasing.com";
  public static String recordPath = "records";
  public static String excelPath = "tmp/staffs.xls";
  public static String outPath = "log/outlog.txt";
  public static String errPath = "log/errlog.txt";
  public static String ekpUser = "sushaolei";
  public static String ekpPass = "11111";
  public static String kkConsoleUser = "admin";
  public static String kkConsolePass = "kk20171110!";
  public static String jobCron = "";
  public static String impTemplateId="";
  public static String xrxs_app_key ="";
  public static String xrxs_app_secret ="";

//  public static String htmlUrl = "";
  public static Map<String, Object> dep_email_id = new HashMap<>();
  public static Map<String, String> familynameMap = new HashMap<>();
  public static Map<String, String> lastnameMap = new HashMap<>();
  public static Map<String, String> tableMap = new HashMap<>();
  public static Map<String, String> tableMap_reverse = new HashMap<>();
  public static Map<String, String> accountMap = new HashMap<>();

  public static String sendMailAccount = "";
  public static String sendMailPassword = "";
  public static String[] receiveMailAccounts = null;
  
  private static  List<String> confText = null;

  public static void init()
    throws Exception
  {
    String conf = "myConf.txt";
    String encode = "UTF-8";
    
    Charset charset = Charset.forName(encode);
    
    if(new File(conf).exists()) {
    	confText = FileUtil.readTxt2List(new FileInputStream(conf), charset);
    	System.out.println("外部配置文件已载入");
    }else {
    	confText = FileUtil.readTxt2List(SysConfig.class.getResourceAsStream("/myConf.txt"), charset);
    	System.out.println("内置配置文件已载入");
    }

    //List<String> confText = FileUtil.readTxt2List(new FileInputStream(conf), encode);
    //List<String> confText = FileUtil.readTxt2List(SysConfig.class.getResourceAsStream(conf), encode);

    boolean findFamilyNameFlag = false;
    boolean findLastNameFlag = false;
    boolean findDeptEmailFlag = false;
    boolean findTableFlag = false;
    boolean findAccountFlag = false;
    for (String confLine : confText) {
      if (!StringUtil.isBlank(confLine)) {
        confLine = confLine.trim();
        if (confLine.contains("#htmlUrl"))
//          htmlUrl = confLine.substring(confLine.indexOf("=") + 1).trim();
        if (confLine.contains("#domainName")) {
          domainName = confLine.substring(confLine.indexOf("=") + 1).trim();
        } else if (confLine.startsWith("#多音字姓{")) {
          findFamilyNameFlag = true;
        } else if (confLine.startsWith("#多音字名{")) {
          findLastNameFlag = true;
        } else if (confLine.startsWith("#明细字段{")) {
          findTableFlag = true;
        } else if (confLine.startsWith("#系统设置{")) {
          findAccountFlag = true;
        } else if (confLine.startsWith("#腾讯邮箱部门ID{")) {
          findDeptEmailFlag = true;
        } else if (confLine.startsWith("#}")) {
          findFamilyNameFlag = false;
          findDeptEmailFlag = false;
          findLastNameFlag = false;
          findTableFlag = false;
          findAccountFlag = false;
        } else if (findFamilyNameFlag) {
          String[] arr = confLine.split("=");
          familynameMap.put(arr[0].replaceFirst("#", ""), arr[1].trim());
        } else if (findDeptEmailFlag) {
          String[] arr = confLine.split("=");
          dep_email_id.put(arr[0].replaceFirst("#", ""), arr[1].trim());
        } else if (findLastNameFlag) {
          String[] arr = confLine.split("=");
          lastnameMap.put(arr[0].replaceFirst("#", ""), arr[1].trim());
        } else if (findTableFlag) {
          String[] arr = confLine.split("=");
          tableMap.put(arr[0].replaceFirst("#", ""), arr[1].trim());
          tableMap_reverse.put(arr[1].trim(), arr[0].replaceFirst("#", ""));
        } else if (findAccountFlag) {
          String[] arr = confLine.split("=");
          accountMap.put(arr[0].replaceFirst("#", ""), arr[1].trim());
        }

      }

    }

    ekpUser = (String)accountMap.get("OA账号");
    ekpPass = (String)accountMap.get("OA密码");
    kkConsoleUser = (String)accountMap.get("KK后台用户");
    kkConsolePass = (String)accountMap.get("KK后台密码");
    excelPath = (String)accountMap.get("excel文件");
    jobCron = (String)accountMap.get("定时表达式");
    impTemplateId = (String)accountMap.get("用户导入模板ID");
    recordPath = (String)accountMap.get("record路径");
    
    String dateStr = DateUtil.date2Str(new Date(), "yyyyMMdd_HHmmss");
    outPath = ((String)accountMap.get("正常日志文件")).replaceAll("\\.", dateStr + ".");
    errPath = ((String)accountMap.get("错误日志文件")).replaceAll("\\.", dateStr + ".");

    sendMailAccount = (String)accountMap.get("发送邮箱");
    sendMailPassword = (String)accountMap.get("发送邮箱密码");
    receiveMailAccounts = ((String)accountMap.get("接收邮箱")).split(";");

    xrxs_app_key = accountMap.get("XRXS_APP_KEY");
    xrxs_app_secret = accountMap.get("XRXS_APP_SECRET");
    
    new File(excelPath).getParentFile().mkdirs();
    
//    System.setErr(new PrintStream(errPath));
//    System.setOut(new PrintStream(outPath));
  }
  
 public static List<String> getConfText() {
	return confText;
}

static
  {
    try
    {
      init();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}